from django.shortcuts import render,redirect
from django.http import HttpResponse
from utils.tecent.cos import delete_bucket,delete_file_list
from web import models
def setting(request,project_id):
    return render(request,'setting.html')

def delete(request,project_id):
    if request.method == 'GET':
        return render(request,'setting_delete.html')
    project_name = request.POST.get('project_name')
    if not project_name :
        return render(request, 'setting_delete.html', {'error': "项目不存在"})
    if  project_name!=request.tracer.project.name:
        return render(request,'setting_delete.html',{'error':"项目名不匹配"})
    #项目删除
    if request.tracer.project.creator!=request.tracer.user:
        return render(request, 'setting_delete.html', {'error': "权限不足，请联系创建者！"})
    #删除桶:文件为空，才能删除桶
    # bucket = request.tracer.project.bucket
    # region = request.tracer.project.region
    # file_list = list_files_in_bucket(bucket, region)
    # delete_file_list(bucket, region, file_list)
    # delete_bucket(bucket, region)
    # # #删除项目
    # # project_name.delete()
    delete_bucket(request.tracer.project.bucket, request.tracer.project.region)
    models.Project.objects.filter(id=request.tracer.project.id).delete()
    return redirect('project_list')